/**
*@author German Barrios @ 01/16/2009
*/
public without sharing class DiscussionMemberController {
	private String discussionId;
	private String userId = null;
	
	/**
	*@constructor
	*/
	public void DiscussionMemberController(){}
	
	/**
	* Add user in a discussion forum
	*/
	public void joinForum(){
		DiscussionMember__c discussionMember = new DiscussionMember__c();
		discussionMember.User__c = this.getUserId();
		discussionMember.DiscussionProfile__c = [select Id from DiscussionProfile__c where Name = 'Member Profile' limit 1].Id;
		discussionMember.DiscussionForum__c = this.getDiscussionId();
		
		insert discussionMember;
	}
	
	/**
	* Quit member of discussion forum
	*/
	public void unjoinForum(){
		List<DiscussionMember__c> member = [select Id, User__c from DiscussionMember__c where User__c =: this.getUserId() and DiscussionForum__c =: this.getDiscussionId()];
		if(member.size() > 0){
			delete member[0];
		}
	}
	
	/**
	* Check if current user is the last member admin
	*@return bool is last member
	*/

	public Boolean isLastMemberAdmin (){
		List<DiscussionMember__c> members = new List<DiscussionMember__c>();
		members = [Select d.Name, User__c From DiscussionMember__c d where DiscussionProfile__r.Name = 'DiscussionForum Administrator' and DiscussionForum__c =: discussionId];
		if(members.size() == 1 && members[0].User__c == getUserId())
			return true;
		else
			return false;		
	}
	
	/**
	* Return current user id
	*@return String
	*/
	public String getUserId(){
		if(userId == null)
			return UserInfo.getUserId();
		else
			return userId;
	}
	
	/**
	*@param id user id
	*/
	public void setUserId(String id){
		userId = id;
	}
	
	/**
	* Set id of discussion forum
	*@param String
	*/
	public void setDiscussionId(String id){
		this.discussionId = id;
	}
	
	/**
	*@return String forum ID
	*/
	public String getDiscussionId(){
		return this.discussionId;
	}
	
	/**
	*@return public profile id
	*/
	public String getDiscussionProfileId(){
		List<DiscussionProfile__c> discussionProfile = [select Id from DiscussionProfile__c where Name = 'Public Profile' ];
		return discussionProfile[0].Id;
	}
}